﻿using System;
using System.Collections.Generic;
using System.Linq;
using Complaint.Models.Dtos;
using Complaint.Repositories;

namespace Complaint.Models
{
    public class ContactModel : BaseModel<ContactRepository>, IModel<Contact>
    {
        #region Implementation of IModel<Contact>

        public IList<Contact> Retrieve()
        {

            return Repository.FindAll().ToList();
        }

        public void Create(Contact dto)
        {


            if (dto != null)
            {
                if (Repository.FindByEmail(dto.Email) == null)
                    throw new ApplicationException("A contact with this email address already exists.");
                Repository.Create(dto);
            }
        }

        public void Update(Contact dto)
        {

            if (dto != null) Repository.Update(dto);
        }

        public void Delete(Contact dto)
        {

            if (dto != null) Repository.Delete(dto);
        }

        public IList<Contact> FindByName(string name)
        {

            return Repository.FindByName(name).ToList();
        }

        public Contact FindById(int id)
        {

            return Repository.FindById(id);
        }

        #endregion

        public Contact FindByEmail(string email)
        {

            return Repository.FindByEmail(email);
        }

        public IList<Contact> FindByOrganisation(int id)
        {

            return Repository.FindByOrganisation(id).ToList();
        }
    }
}